package com.smm.web.mtower.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Created by zhenghao on 2016/6/24.
 *
 */
public class UrlUtil {


    /**
     * get方式请求指定url ，并将响应结果作为string返回
     * @param httpUrl   目标url
     * @return
     * @throws IOException
     */
    public static String getContent(String httpUrl) throws IOException {

        URL url = new URL(httpUrl);
        InputStream inputStream = url.openStream();
        InputStreamReader inputReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputReader);
        StringBuilder sb = new StringBuilder();
        String str;

        do {
            str = reader.readLine();
            sb.append(str);
        } while (null != str);

        str = sb.toString();

        reader.close();
        inputReader.close();
        inputStream.close();

        if (str.isEmpty()) {
            return null;
        }

        return str;
    }
}
